/*     */ package com.cr.system.service.impl;
/*     */ 
/*     */ import com.cr.common.annotation.DataScope;
/*     */ import com.cr.common.core.domain.entity.SysRole;
/*     */ import com.cr.common.core.domain.entity.SysUser;
/*     */ import com.cr.common.core.text.Convert;
/*     */ import com.cr.common.exception.ServiceException;
/*     */ import com.cr.common.utils.ShiroUtils;
/*     */ import com.cr.common.utils.MyStringUtils;
/*     */ import com.cr.common.utils.security.Md5Utils;
/*     */ import com.cr.common.utils.spring.SpringUtils;
/*     */ import com.cr.system.domain.SysPost;
/*     */ import com.cr.system.domain.SysUserPost;
/*     */ import com.cr.system.domain.SysUserRole;
/*     */ import com.cr.system.mapper.SysPostMapper;
/*     */ import com.cr.system.mapper.SysRoleMapper;
/*     */ import com.cr.system.mapper.SysUserMapper;
/*     */ import com.cr.system.mapper.SysUserPostMapper;
/*     */ import com.cr.system.mapper.SysUserRoleMapper;
/*     */ import com.cr.system.service.ISysConfigService;
/*     */ import com.cr.system.service.ISysUserService;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import org.slf4j.Logger;
/*     */ import org.slf4j.LoggerFactory;
/*     */ import org.springframework.beans.factory.annotation.Autowired;
/*     */ import org.springframework.stereotype.Service;
/*     */ import org.springframework.transaction.annotation.Transactional;
/*     */ 
/*     */ @Service
/*     */ public class SysUserServiceImpl implements ISysUserService {
/*  39 */   private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
/*     */   
/*     */   @Autowired
/*     */   private SysUserMapper userMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysRoleMapper roleMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysPostMapper postMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysUserPostMapper userPostMapper;
/*     */   
/*     */   @Autowired
/*     */   private SysUserRoleMapper userRoleMapper;
/*     */   
/*     */   @Autowired
/*     */   private ISysConfigService configService;
/*     */   
/*     */   @DataScope(deptAlias = "d", userAlias = "u")
/*     */   public List<SysUser> selectUserList(SysUser user) {
/*  69 */     return this.userMapper.selectUserList(user);
/*     */   }
/*     */   
/*     */   @DataScope(deptAlias = "d", userAlias = "u")
/*     */   public List<SysUser> selectAllocatedList(SysUser user) {
/*  82 */     return this.userMapper.selectAllocatedList(user);
/*     */   }
/*     */   
/*     */   @DataScope(deptAlias = "d", userAlias = "u")
/*     */   public List<SysUser> selectUnallocatedList(SysUser user) {
/*  95 */     return this.userMapper.selectUnallocatedList(user);
/*     */   }
/*     */   
/*     */   public SysUser selectUserByLoginName(String userName) {
/* 107 */     return this.userMapper.selectUserByLoginName(userName);
/*     */   }
/*     */   
/*     */   public SysUser selectUserByPhoneNumber(String phoneNumber) {
/* 119 */     return this.userMapper.selectUserByPhoneNumber(phoneNumber);
/*     */   }
/*     */   
/*     */   public SysUser selectUserByEmail(String email) {
/* 131 */     return this.userMapper.selectUserByEmail(email);
/*     */   }
/*     */   
/*     */   public SysUser selectUserById(Long userId) {
/* 143 */     return this.userMapper.selectUserById(userId);
/*     */   }
/*     */   
/*     */   public List<SysUserRole> selectUserRoleByUserId(Long userId) {
/* 155 */     return this.userRoleMapper.selectUserRoleByUserId(userId);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int deleteUserById(Long userId) {
/* 169 */     this.userRoleMapper.deleteUserRoleByUserId(userId);
/* 171 */     this.userPostMapper.deleteUserPostByUserId(userId);
/* 172 */     return this.userMapper.deleteUserById(userId);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int deleteUserByIds(String ids) {
/* 185 */     Long[] userIds = Convert.toLongArray(ids);
/* 186 */     for (Long userId : userIds)
/* 188 */       checkUserAllowed(new SysUser(userId)); 
/* 191 */     this.userRoleMapper.deleteUserRole(userIds);
/* 193 */     this.userPostMapper.deleteUserPost(userIds);
/* 194 */     return this.userMapper.deleteUserByIds(userIds);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int insertUser(SysUser user) {
/* 208 */     int rows = this.userMapper.insertUser(user);
/* 210 */     insertUserPost(user);
/* 212 */     insertUserRole(user.getUserId(), user.getRoleIds());
/* 213 */     return rows;
/*     */   }
/*     */   
/*     */   public boolean registerUser(SysUser user) {
/* 225 */     user.setUserType("01");
/* 226 */     return (this.userMapper.insertUser(user) > 0);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public int updateUser(SysUser user) {
/* 239 */     Long userId = user.getUserId();
/* 241 */     this.userRoleMapper.deleteUserRoleByUserId(userId);
/* 243 */     insertUserRole(user.getUserId(), user.getRoleIds());
/* 245 */     this.userPostMapper.deleteUserPostByUserId(userId);
/* 247 */     insertUserPost(user);
/* 248 */     return this.userMapper.updateUser(user);
/*     */   }
/*     */   
/*     */   public int updateUserInfo(SysUser user) {
/* 260 */     return this.userMapper.updateUser(user);
/*     */   }
/*     */   
/*     */   @Transactional
/*     */   public void insertUserAuth(Long userId, Long[] roleIds) {
/* 273 */     this.userRoleMapper.deleteUserRoleByUserId(userId);
/* 274 */     insertUserRole(userId, roleIds);
/*     */   }
/*     */   
/*     */   public int resetUserPwd(SysUser user) {
/* 286 */     return updateUserInfo(user);
/*     */   }
/*     */   
/*     */   public void insertUserRole(Long userId, Long[] roleIds) {
/* 297 */     if (MyStringUtils.isNotNull(roleIds)) {
/* 300 */       List<SysUserRole> list = new ArrayList<>();
/* 301 */       for (Long roleId : roleIds) {
/* 303 */         SysUserRole ur = new SysUserRole();
/* 304 */         ur.setUserId(userId);
/* 305 */         ur.setRoleId(roleId);
/* 306 */         list.add(ur);
/*     */       } 
/* 308 */       if (list.size() > 0)
/* 310 */         this.userRoleMapper.batchUserRole(list); 
/*     */     } 
/*     */   }
/*     */   
/*     */   public void insertUserPost(SysUser user) {
/* 322 */     Long[] posts = user.getPostIds();
/* 323 */     if (MyStringUtils.isNotNull(posts)) {
/* 326 */       List<SysUserPost> list = new ArrayList<>();
/* 327 */       for (Long postId : posts) {
/* 329 */         SysUserPost up = new SysUserPost();
/* 330 */         up.setUserId(user.getUserId());
/* 331 */         up.setPostId(postId);
/* 332 */         list.add(up);
/*     */       } 
/* 334 */       if (list.size() > 0)
/* 336 */         this.userPostMapper.batchUserPost(list); 
/*     */     } 
/*     */   }
/*     */   
/*     */   public String checkLoginNameUnique(String loginName) {
/* 350 */     int count = this.userMapper.checkLoginNameUnique(loginName);
/* 351 */     if (count > 0)
/* 353 */       return "1"; 
/* 355 */     return "0";
/*     */   }
/*     */   
/*     */   public String checkPhoneUnique(SysUser user) {
/* 367 */     Long userId = Long.valueOf(MyStringUtils.isNull(user.getUserId()) ? -1L : user.getUserId().longValue());
/* 368 */     SysUser info = this.userMapper.checkPhoneUnique(user.getPhonenumber());
/* 369 */     if (MyStringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
/* 371 */       return "1"; 
/* 373 */     return "0";
/*     */   }
/*     */   
/*     */   public String checkEmailUnique(SysUser user) {
/* 385 */     Long userId = Long.valueOf(MyStringUtils.isNull(user.getUserId()) ? -1L : user.getUserId().longValue());
/* 386 */     SysUser info = this.userMapper.checkEmailUnique(user.getEmail());
/* 387 */     if (MyStringUtils.isNotNull(info) && info.getUserId().longValue() != userId.longValue())
/* 389 */       return "1"; 
/* 391 */     return "0";
/*     */   }
/*     */   
/*     */   public void checkUserAllowed(SysUser user) {
/* 402 */     if (MyStringUtils.isNotNull(user.getUserId()) && user.isAdmin())
/* 404 */       throw new ServiceException("不允许操作超级管理员用户"); 
/*     */   }
/*     */   
/*     */   public void checkUserDataScope(Long userId) {
/* 416 */     if (!SysUser.isAdmin(ShiroUtils.getUserId())) {
/* 418 */       SysUser user = new SysUser();
/* 419 */       user.setUserId(userId);
/* 420 */       List<SysUser> users = ((SysUserServiceImpl)SpringUtils.getAopProxy(this)).selectUserList(user);
/* 421 */       if (MyStringUtils.isEmpty(users))
/* 423 */         throw new ServiceException("没有权限访问用户数据！"); 
/*     */     } 
/*     */   }
/*     */   
/*     */   public String selectUserRoleGroup(Long userId) {
/* 437 */     List<SysRole> list = this.roleMapper.selectRolesByUserId(userId);
/* 438 */     StringBuffer idsStr = new StringBuffer();
/* 439 */     for (SysRole role : list)
/* 441 */       idsStr.append(role.getRoleName()).append(","); 
/* 443 */     if (MyStringUtils.isNotEmpty(idsStr.toString()))
/* 445 */       return idsStr.substring(0, idsStr.length() - 1); 
/* 447 */     return idsStr.toString();
/*     */   }
/*     */   
/*     */   public String selectUserPostGroup(Long userId) {
/* 459 */     List<SysPost> list = this.postMapper.selectPostsByUserId(userId);
/* 460 */     StringBuffer idsStr = new StringBuffer();
/* 461 */     for (SysPost post : list)
/* 463 */       idsStr.append(post.getPostName()).append(","); 
/* 465 */     if (MyStringUtils.isNotEmpty(idsStr.toString()))
/* 467 */       return idsStr.substring(0, idsStr.length() - 1); 
/* 469 */     return idsStr.toString();
/*     */   }
/*     */   
/*     */   public String importUser(List<SysUser> userList, Boolean isUpdateSupport, String operName) {
/* 483 */     if (MyStringUtils.isNull(userList) || userList.size() == 0)
/* 485 */       throw new ServiceException("导入用户数据不能为空！"); 
/* 487 */     int successNum = 0;
/* 488 */     int failureNum = 0;
/* 489 */     StringBuilder successMsg = new StringBuilder();
/* 490 */     StringBuilder failureMsg = new StringBuilder();
/* 491 */     String password = this.configService.selectConfigByKey("sys.user.initPassword");
/* 492 */     for (SysUser user : userList) {
/*     */       try {
/* 497 */         SysUser u = this.userMapper.selectUserByLoginName(user.getLoginName());
/* 498 */         if (MyStringUtils.isNull(u)) {
/* 500 */           user.setPassword(Md5Utils.hash(user.getLoginName() + password));
/* 501 */           user.setCreateBy(operName);
/* 502 */           insertUser(user);
/* 503 */           successNum++;
/* 504 */           successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
/*     */           continue;
/*     */         } 
/* 506 */         if (isUpdateSupport.booleanValue()) {
/* 508 */           user.setUpdateBy(operName);
/* 509 */           updateUser(user);
/* 510 */           successNum++;
/* 511 */           successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 更新成功");
/*     */           continue;
/*     */         } 
/* 515 */         failureNum++;
/* 516 */         failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
/* 519 */       } catch (Exception e) {
/* 521 */         failureNum++;
/* 522 */         String msg = "<br/>" + failureNum + "、账号 " + user.getLoginName() + " 导入失败：";
/* 523 */         failureMsg.append(msg + e.getMessage());
/* 524 */         log.error(msg, e);
/*     */       } 
/*     */     } 
/* 527 */     if (failureNum > 0) {
/* 529 */       failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
/* 530 */       throw new ServiceException(failureMsg.toString());
/*     */     } 
/* 534 */     successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
/* 536 */     return successMsg.toString();
/*     */   }
/*     */   
/*     */   public int changeStatus(SysUser user) {
/* 548 */     return this.userMapper.updateUser(user);
/*     */   }
/*     */ }


/* Location:              D:\tmp\creditorRights-admin\BOOT-INF\lib\creditorRights-system-4.7.0.jar!\com\cr\system\service\impl\SysUserServiceImpl.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */